package quan.dong.pricealert;

import quan.dong.common.Common;
import quan.dong.common.CommonEnums;
import quan.dong.common.Constant;
import quan.dong.handle.Login;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

public class SplashScreen extends Activity {

	// Splash screen timer
	NotificationManager mNotificationManager;
	
	private static int SPLASH_TIME_OUT = 2000;
	Handler waitHandler=new Handler();
	boolean isLoginActivity = false;
	TextView tvCaution;
	Intent i;
	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Constant.HANDLER_LOGIN_RESULT:
				switch (CommonEnums.RetCode.values()[(Integer) msg.obj]) {
				case Banned:
					tvCaution.setText(getString(R.string.your_account_has_been_disabled));
					break;
				case Expired:
					Toast.makeText(getBaseContext(), "Expired",
							Toast.LENGTH_SHORT).show();					
					break;
				case Success:
					i = new Intent(SplashScreen.this, MainActivity.class);
					waitHandler.postDelayed(waitTime,SPLASH_TIME_OUT);
					break;
				case CreateUserSuccess:
					Toast.makeText(getBaseContext(), "CreateUserSuccess",
							Toast.LENGTH_SHORT).show();
					break;
				case SystemError:
					if(Common.isNetworkAvailable(getBaseContext())){
						tvCaution
						.setText(getString(R.string.system_error));
					}else
					{
						tvCaution.setText(getString(R.string.network_not_available));
					}
					
					break;
				default:
					break;
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		tvCaution = (TextView) findViewById(R.id.txCaution);
		if (Common.isNetworkAvailable(getBaseContext())) {
			Login login = new Login(Common.GetImeiNumber(getBaseContext()),
					Common.GetPhoneNumber(getBaseContext()), handler,
					"LoginThread");
		} else {
			tvCaution.setText(getString(R.string.network_not_available));
		}
		
	}

	Runnable waitTime = new Runnable() {

		@Override
		public void run() {			
			startActivity(i);
			finish();
		}
	};
}